package test.my.com.myapplicationtest.roll_number;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import test.my.com.myapplicationtest.R;

/**
 * Author : Cooper
 * Time : 2017/2/15  17:10
 * Description : 横向的多位ScrollNumber,带有小数。
 *               赚呗1.2.0之前版本数字滚动。
 *               1.可以分别设置整数位和小数位字体大小。2.整数位可以自动进位和添加分隔符。
 */

public class HorizontalMultiScrollNumber extends LinearLayout {

    private MultiScrollNumber mIntegerMsn;
    private MultiScrollNumber mDecimalMsn;
    private int mNumberColor = Color.BLACK;
    private int mIntegerNumberSize = 50;
    private int mDecimalNumberSize = 50;
    private int mIntervalTime;
    private Context mContext;
    public HorizontalMultiScrollNumber(Context context) {
        super(context);
        initView(context);
    }

    public HorizontalMultiScrollNumber(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalMultiScrollNumber(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.HorizontalMultiScrollNumber,defStyleAttr,0);
        mNumberColor = typedArray.getColor(R.styleable.HorizontalMultiScrollNumber_horizontalNumberColor, Color.BLACK);
        mIntegerNumberSize = typedArray.getDimensionPixelSize(R.styleable.HorizontalMultiScrollNumber_integerNumberSize, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20,
                getResources().getDisplayMetrics()));
        mDecimalNumberSize = typedArray.getDimensionPixelSize(R.styleable.HorizontalMultiScrollNumber_decimalNumberSize, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20,
                getResources().getDisplayMetrics()));
        mIntervalTime = typedArray.getInteger(R.styleable.HorizontalMultiScrollNumber_horizontalIntervalTime, 500);
        typedArray.recycle();
        initView(context);
    }

    private void initView(Context context) {
        this.mContext = context;
        mIntegerMsn = new MultiScrollNumber(mContext);
        mIntegerMsn.setTextSize(mIntegerNumberSize);
        mIntegerMsn.setMultiTextColor(mNumberColor);
        mIntegerMsn.setDecollator(",");
        mDecimalMsn = new MultiScrollNumber(mContext);
        mDecimalMsn.setTextSize(mDecimalNumberSize);
        mDecimalMsn.setMultiTextColor(mNumberColor);
        mDecimalMsn.showDecimalPoint(true);
        this.setGravity(Gravity.BOTTOM);
        addView(mIntegerMsn);
        addView(mDecimalMsn);
    }

    public void resetView() {
        mDecimalMsn.resetView();
        mIntegerMsn.resetView();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

    }

    public void setNumber(String number) {
        if (TextUtils.isEmpty(number) || !number.contains(".")) {
            return;
        }
        String[] targetNumber = splitTotalIncome(number);
        mIntegerMsn.setNumbers(targetNumber[0]);
        mDecimalMsn.setNumbers(targetNumber[1]);

    }

    /**
     * 拆分数字为整数部分和小数部分
     *
     * @param data
     * @return
     */
    private String[] splitTotalIncome(String data) {
        return data.split("\\.");
    }
}